if(NOT userver_FIND_COMPONENTS)
  set(userver_FIND_COMPONENTS @USERVER_AVAILABLE_COMPONENTS@)
endif()
list(REMOVE_DUPLICATES userver_FIND_COMPONENTS)

foreach(ITEM_COMPONENT IN LISTS userver_FIND_COMPONENTS)
  if(TARGET userver::${ITEM_COMPONENT})
    get_target_property(is_imported userver::${ITEM_COMPONENT} IMPORTED)
    if(NOT is_imported)
      # Support `find_package(userver ...)` if userver was previously included
      # as subdirectory
      set(${ITEM_COMPONENT}_FOUND TRUE)
    endif()
    if(${ITEM_COMPONENT}_FOUND)
      list(REMOVE_ITEM userver_FIND_COMPONENTS ${ITEM_COMPONENT})
    endif()
  endif()
endforeach()

if (NOT userver_FIND_COMPONENTS)
  # UserverSetupEnvironment.cmake was already included (otherwise the
  # userver::universal target could not been build).
  userver_setup_environment()
  return()
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
  include("${CMAKE_CURRENT_LIST_DIR}/release/userver-targets.cmake")
else()
  include("${CMAKE_CURRENT_LIST_DIR}/debug/userver-targets_d.cmake")
endif()

message(STATUS "Userver components: ${userver_FIND_COMPONENTS}")

include(CMakeFindDependencyMacro)

set(USERVER_DOWNLOAD_PACKAGES OFF)
set(USERVER_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR})
set(USERVER_TESTSUITE_DIR "${USERVER_CMAKE_DIR}/testsuite")
set(USERVER_IMPL_ORIGINAL_CXX_STANDARD @CMAKE_CXX_STANDARD@)
set(USERVER_IMPL_FEATURE_JEMALLOC @USERVER_FEATURE_JEMALLOC@)

list(APPEND CMAKE_MODULE_PATH "${USERVER_CMAKE_DIR}")

foreach(ITEM_COMPONENT IN LISTS userver_FIND_COMPONENTS)
  include("${USERVER_CMAKE_DIR}/userver-${ITEM_COMPONENT}-config.cmake")
endforeach()

@PACKAGE_INIT@

check_required_components(userver)
